package net.adst.beans;

import net.adst.util.MyUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.util.HSSFColor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 05.11.12
 * Time: 9:57
 * To change this template use File | Settings | File Templates.
 */
public class NaemTec {
    private Date date;
    private String chIN;
    private String chOut;
    private String tecName;
    private String managerName;
    private String customer;
    private String priceHoursCust;
    private String vsego;
    private String summaPoSchety;
    private String hours;
    private String dohod;
    private String performer;
    private String priceHoursOur;
    private String hoursOur;
    private String rashod;
    private String profit;
    private String objectName;
    private String arenda;
    private String prorab;
    private String comment;
    private Boolean paidNam;
    /*
    0 - не известно(белый)
    1 - надо оплатить, деньги пришли(фиолетовый)
    2 - мы оплатили(желтый)
     */
    private Integer moneyIn;
    private Integer rowInFile;

    public NaemTec(HSSFRow row) {
        if (row.getCell(0).getCellStyle().getFillForegroundColor() == HSSFColor.YELLOW.index) {
            paidNam = true;
        } else {
            paidNam = false;
        }


        if (row.getCell(0).toString() != null) {
            this.date = row.getCell(0).getDateCellValue();
        } else {
            this.date = new Date();
        }
        if (row.getCell(1).toString() != null) {
            this.chIN = row.getCell(1).toString();
        } else {
            this.chIN = "";
        }
        if (row.getCell(2).toString() != null) {
            this.chOut = row.getCell(2).toString();
        } else {
            this.chOut = "";
        }

        if (row.getCell(3).toString() != null) {
            this.tecName = row.getCell(3).toString();
        } else {
            this.tecName = "";
        }
        if (row.getCell(4).toString() != null) {
            this.managerName = row.getCell(4).toString();
        } else {
            this.managerName = "";
        }
        if (row.getCell(5).toString() != null) {
            this.customer = row.getCell(5).toString();
        } else {
            this.customer = "";
        }
        if (row.getCell(6).toString() != null) {
            this.priceHoursCust = row.getCell(6).toString();
        } else {
            this.priceHoursCust = "";
        }
        if (row.getCell(7).toString() != null) {
            this.vsego = row.getCell(7).toString();
        } else {
            this.vsego = "";
        }
        if (row.getCell(8).toString() != null) {
            this.summaPoSchety = row.getCell(8).toString();
        } else {
            this.summaPoSchety = "";
        }
        if (row.getCell(9).toString() != null) {
            this.hours = row.getCell(9).toString();
        } else {
            this.hours = "";
        }
        if ((row.getCell(6).toString() != null) && (row.getCell(9).toString() != null) && (!row.getCell(6).toString().isEmpty()) && (!row.getCell(9).toString().isEmpty())) {
            Double priceHour = new Double(row.getCell(6).toString());
            Double hour = new Double(row.getCell(9).toString());
            this.dohod = String.valueOf(priceHour * hour);
        } else {
            this.dohod = "";
        }
        if (row.getCell(11).toString() != null) {
            this.performer = row.getCell(11).toString();
        } else {
            this.performer = "";
        }
        if (row.getCell(11).getCellStyle().getFillForegroundColor() == HSSFColor.VIOLET.index) {
            moneyIn = 1;
        } else if (row.getCell(11).getCellStyle().getFillForegroundColor() == HSSFColor.YELLOW.index) {
            moneyIn = 2;
        } else {
            moneyIn = 0;
        }
        if (row.getCell(12).toString() != null) {
            this.priceHoursOur = row.getCell(12).toString();
        } else {
            this.priceHoursOur = "";
        }
        if (row.getCell(13).toString() != null) {
            this.hoursOur = row.getCell(13).toString();
        } else {
            this.hoursOur = "";
        }
        if ((row.getCell(12).toString() != null) && (row.getCell(13).toString() != null) && (!row.getCell(12).toString().isEmpty()) && (!row.getCell(13).toString().isEmpty())) {
            Double priceHour = new Double(row.getCell(12).toString());
            Double hour = new Double(row.getCell(13).toString());
            this.rashod = String.valueOf(priceHour * hour);
        } else {
            this.rashod = "";
        }
        if (row.getCell(16).toString() != null) {
            this.objectName = row.getCell(16).toString();
        } else {
            this.objectName = "";
        }
        if (row.getCell(17).toString() != null) {
            this.arenda = row.getCell(17).toString();
        } else {
            this.arenda = "";
        }
        if (row.getCell(18).toString() != null) {
            if (!row.getCell(18).toString().contains("*")) {
                this.prorab = row.getCell(18).toString();
            } else {
                String value = row.getCell(18).toString();
                this.prorab = String.valueOf(Double.valueOf(row.getCell(MyUtils.getAbcNum(value.toCharArray()[0])).toString()) * Double.valueOf((value.substring(value.lastIndexOf('*') + 1))));
            }
        } else {
            this.prorab = "";
        }
        if ((!dohod.equals("")) && (!rashod.equals(""))) {
            if (!prorab.isEmpty()) {
                Double doh = dohod.isEmpty() ? new Double("0") : Double.valueOf(dohod);
                Double ras = rashod.isEmpty() ? new Double("0") : Double.valueOf(rashod);
                Double ar = arenda.isEmpty() ? new Double("0") : Double.valueOf(arenda);
                Double pr = prorab.isEmpty() ? new Double("0") : Double.valueOf(prorab);
                this.profit = String.valueOf(doh - ras - ar - pr);
            } else {
                this.profit = String.valueOf(Double.valueOf(dohod) - Double.valueOf(rashod));
            }
        } else {
            this.profit = "";
        }
        if (row.getCell(19) != null || row.getCell(20) != null || row.getCell(21) != null || row.getCell(22) != null || row.getCell(23) != null || row.getCell(24) != null || row.getCell(25) != null || row.getCell(26) != null || row.getCell(27) != null) {
            this.comment = row.getCell(19) != null ? row.getCell(19).toString() : "";
            for (int i = 20; i < 27; i++) {
                if (row.getCell(i) != null && !row.getCell(i).toString().equals("")) {
                    this.comment = this.comment.equals("") ? row.getCell(i).toString() : this.comment + "\n" + row.getCell(i).toString();
                }
            }
            if (this.comment == null) {
                this.comment = "";
            }
        } else {
            this.comment = "";
        }
        this.rowInFile = row.getRowNum();
    }

    public NaemTec(Date date, String chIN, String chOut, String tecName, String managerName, String customer, String priceHoursCust, String vsego, String summaPoSchety, String hoursCust, String income, String performer, String priceHoursOur, String hoursOur, String amountOur, String profit, String objectName, String arenda, String prorab, String comment, Integer rowInFile) {
        this.date = date;
        this.chIN = chIN;
        this.chOut = chOut;
        this.tecName = tecName;
        this.managerName = managerName;
        this.customer = customer;
        this.priceHoursCust = priceHoursCust;
        this.vsego = vsego;
        this.summaPoSchety = summaPoSchety;
        this.hours = hoursCust;
        this.dohod = income;
        this.performer = performer;
        this.priceHoursOur = priceHoursOur;
        this.hoursOur = hoursOur;
        this.rashod = amountOur;
        this.profit = profit;
        this.objectName = objectName;
        this.arenda = arenda;
        this.prorab = prorab;
        this.comment = comment;
        this.rowInFile = rowInFile;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getChIN() {
        return chIN;
    }

    public void setChIN(String chIN) {
        this.chIN = chIN;
    }

    public String getChOut() {
        return chOut;
    }

    public void setChOut(String chOut) {
        this.chOut = chOut;
    }

    public String getTecName() {
        return tecName;
    }

    public void setTecName(String tecName) {
        this.tecName = tecName;
    }

    public String getManagerName() {
        return managerName;
    }

    public void setManagerName(String managerName) {
        this.managerName = managerName;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getPriceHoursCust() {
        return priceHoursCust;
    }

    public void setPriceHoursCust(String priceHoursCust) {
        this.priceHoursCust = priceHoursCust;
    }

    public String getVsego() {
        return vsego;
    }

    public void setVsego(String vsego) {
        this.vsego = vsego;
    }

    public String getSummaPoSchety() {
        return summaPoSchety;
    }

    public void setSummaPoSchety(String summaPoSchety) {
        this.summaPoSchety = summaPoSchety;
    }

    public String getHours() {
        return hours;
    }

    public void setHours(String hours) {
        this.hours = hours;
    }

    public String getDohod() {
        return dohod;
    }

    public void setDohod(String dohod) {
        this.dohod = dohod;
    }

    public String getPerformer() {
        return performer;
    }

    public void setPerformer(String performer) {
        this.performer = performer;
    }

    public String getPriceHoursOur() {
        return priceHoursOur;
    }

    public void setPriceHoursOur(String priceHoursOur) {
        this.priceHoursOur = priceHoursOur;
    }

    public String getHoursOur() {
        return hoursOur;
    }

    public void setHoursOur(String hoursOur) {
        this.hoursOur = hoursOur;
    }

    public String getRashod() {
        return rashod;
    }

    public void setRashod(String rashod) {
        this.rashod = rashod;
    }

    public String getProfit() {
        return profit;
    }

    public void setProfit(String profit) {
        this.profit = profit;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public String getArenda() {
        return arenda;
    }

    public void setArenda(String arenda) {
        this.arenda = arenda;
    }

    public String getProrab() {
        return prorab;
    }

    public void setProrab(String prorab) {
        this.prorab = prorab;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Integer getRowInFile() {
        return rowInFile;
    }

    public void setRowInFile(Integer rowInFile) {
        this.rowInFile = rowInFile;
    }

    public Boolean getPaidNam() {
        return paidNam;
    }

    public void setPaidNam(Boolean paidNam) {
        this.paidNam = paidNam;
    }

    public Integer getMoneyIn() {
        return moneyIn;
    }

    public void setMoneyIn(Integer moneyIn) {
        this.moneyIn = moneyIn;
    }

    public Object[] toRow() {
        return new Object[]{(new SimpleDateFormat("dd.MM.yyyy")).format(date.getTime()), chIN, chOut, tecName, managerName, customer, priceHoursCust, vsego, summaPoSchety, hours, dohod, performer, priceHoursOur, hoursOur, rashod, profit, objectName, arenda, prorab, comment.equals("") ? "" : "*", rowInFile};
    }

    public boolean isEmpty() {
        if (!chIN.isEmpty() || !chOut.isEmpty() || !tecName.isEmpty() || (!managerName.isEmpty() && !managerName.equals("-")) || !customer.isEmpty() || (!priceHoursCust.isEmpty() && !priceHoursCust.equals("0.0")) || !hours.isEmpty() || (!rashod.isEmpty() && !rashod.equals("0.0")) || !performer.isEmpty() || (!priceHoursOur.isEmpty() && !priceHoursOur.equals("0.0")) || !hoursOur.isEmpty() || (!profit.isEmpty() && !profit.equals("0.0")) || !objectName.isEmpty() || !prorab.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }
}
